const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

const min = 1;
const max = 100;
let secretNumber;
let attempts = 0;
const maxAttempts = 7;

function startGame() {
  secretNumber = Math.floor(Math.random() * (max - min + 1)) + min;
  attempts = 0;
  
  console.log('🎮 猜数字游戏开始！');
  console.log(` 我想了一个 ${min} 到 ${max} 之间的数字`);
  console.log(` 你有 ${maxAttempts} 次机会\n`);
  
  askGuess();
}

function askGuess() {
  if (attempts >= maxAttempts) {
    console.log(`❌ 游戏结束！正确答案是: ${secretNumber}`);
    playAgain();
    return;
  }

  rl.question(`第 ${attempts + 1} 次猜测: `, (input) => {
    const guess = parseInt(input);

    if (isNaN(guess)) {
      console.log('❌ 请输入数字\n');
      askGuess();
      return;
    }

    attempts++;
    
    if (guess === secretNumber) {
      console.log(`🎉 恭喜！用了 ${attempts} 次猜中`);
      playAgain();
    } else if (guess < secretNumber) {
      console.log('📈 太小了\n');
      askGuess();
    } else {
      console.log('📉 太大了\n');
      askGuess();
    }
  });
}
startGame();